const app = getApp()
let qCloud = require("../../../index.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        tabIndex: 1,

        userId: null,
        userNameTxt: "请选择员工",
        phoneTxt: "系统自动获取",

        deptId: null,
        deptNameTxt: "请选择部门",
        postId: null,
        postNameTxt: "请选择岗位",


        menuList: [],
        manageMenuIds: [],

        wagesForm: {
            basicWages: 0,
            calcWages: 0,
            commission: 0,
            other: 0,
            fullPraise: 0,
            subsidy: 0,
            deductFood: 0,
            deductWaterCable: 0
        },

        queryPostParams: {
            pageNum: 1,
            pageSize: 20,
            postName: ''
        },


        userList: [],


        postList: [],
        form: {}

    },

    // 顶部菜单选择
    tabClick(e) {
        this.setData({
            tabIndex: e.currentTarget.dataset.index,
            postList: [],
            userList: [],
        });

        if (e.currentTarget.dataset.index == 1) {
            this.getMyOrganUserList();
        }

        if (e.currentTarget.dataset.index == 2) {
            this.getPostList(this.data.queryPostParams);
        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.onUnload();
        this.getMyOrganUserList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let self = this;
        if (self.data.tabIndex == 1) {
            this.getMyOrganUserList();
        }

        if (self.data.tabIndex == 2) {
            self.setData({
                queryPostParams: {
                    pageNum: 1,
                    pageSize: 20,
                    postName: '',
                },
                postList: [],
            })
            this.getPostList(self.data.queryPostParams);
        }

        if (self.data.tabIndex == 3) {
            let userInfo = wx.getStorageSync("weapp_choose_user");
            if (userInfo != null) {
                this.setData({
                    userId: userInfo.userId,
                    userNameTxt: userInfo.realName,
                    phoneTxt: userInfo.phone,
                    deptId: userInfo.deptId,
                    deptNameTxt: userInfo.deptName,
                    postId: userInfo.postId,
                    postNameTxt: userInfo.postName
                });

                if (userInfo.postId != null) {
                    app.fetchApis(self, qCloud.api.getPostInfo + userInfo.postId, {}, "GET", function (e) {
                        if (e.data.code == 200) {
                            let params = {
                                resourceId: 10004,
                                menuType: "C",
                                tagType: '0',
                                roleId: e.data.data.manageRoleId
                            }
                            app.fetchApis(self, qCloud.api.getMenuTypeAll_C, params, "GET", function (e) {
                                if (e.data.code == 200) {
                                    self.setData({
                                        menuList: e.data.data.checkedMenuList
                                    })
                                }
                            })
                        }
                    })
                }

            }

            let deptInfo = wx.getStorageSync("weapp_choose_dept");
            if (deptInfo != null) {
                this.setData({
                    deptId: deptInfo.deptId,
                    deptNameTxt: deptInfo.deptName
                })
            }

            let postInfo = wx.getStorageSync("weapp_choose_post");
            if (postInfo != null) {
                this.setData({
                    postId: postInfo.postId,
                    postNameTxt: postInfo.postName
                });

                app.fetchApis(self, qCloud.api.getPostInfo + postInfo.postId, {}, "GET", function (e) {
                    if (e.data.code == 200) {
                        let params = {
                            resourceId: 10004,
                            menuType: "C",
                            tagType: '0',
                            roleId: e.data.data.manageRoleId
                        }

                        app.fetchApis(self, qCloud.api.getMenuTypeAll_C, params, "GET", function (e) {
                            if (e.data.code == 200) {
                                self.setData({
                                    menuList: e.data.data.checkedMenuList
                                })
                            }
                        })
                    }
                })
            }
        }
    },


    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.removeStorageSync("weapp_choose_user");
        wx.removeStorageSync("weapp_choose_dept");
        wx.removeStorageSync("weapp_choose_post");
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let self = this;
        if (self.data.tabIndex == 1) {
            this.getMyOrganUserList();
        }

        if (self.data.tabIndex == 2) {
            self.setData({
                queryPostParams: {
                    pageNum: 1,
                    pageSize: 20,
                    postName: '',
                },
                postList: []
            });
            this.getPostList(self.data.queryPostParams);
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let self = this;
        if (self.data.tabIndex == 2) {
            if (self.data.queryPostParams.pageSize == self.data.postList.length) {
                self.setData({
                    queryPostParams: {
                        pageNum: self.data.queryPostParams.pageNum + 1,
                        pageSize: 20,
                    }
                });
                self.getPostList(self.data.queryPostParams);
            } else {
                return;
            }
        }
    },

    searchUserInput: function (e) {
        let self = this;
        if (e.detail.value != '') {
            if (this.data.userList.length > 0) {
                let userList = [];
                for (let i = 0; i < this.data.userList.length; i++) {
                    let user = this.data.userList[i];
                    if (user.realName.indexOf(e.detail.value) >= 0) {
                        userList.push(this.data.userList[i])
                    }
                }
                self.setData({
                    userList: userList
                })
            }
        } else {
            this.getMyOrganUserList();
        }
    },

    getMyOrganUserList: function () {
        let self = this;
        app.fetchApis(self, qCloud.api.getMyOrganUserList, {}, "GET", function (e) {
            if (e.data.code == 200) {
                self.setData({
                    userList: e.data.data
                })
            }
        })
    },


    /**用户长按删除*/
    userLongTap: function (e) {
        let self = this;
        let url = qCloud.api.deleteUser + e.currentTarget.dataset.userid;
        wx.showModal({
            content: '确定要删除员工吗？',
            complete: (res) => {
                if (res.confirm) {
                    app.fetchApis(self, url, {}, "POST", function (e) {
                        if (e.data.code == 200) {
                            app.fetchApis(self, qCloud.api.getMyOrganUserList, {}, "GET", function (e) {
                                if (e.data.code == 200) {
                                    self.setData({
                                        userList: e.data.data
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: "error"
                            })
                        }
                    })
                }

                if (res.cancel) {
                    app.fetchApis(self, qCloud.api.getMyOrganUserList, {}, "GET", function (e) {
                        if (e.data.code == 200) {
                            self.setData({
                                userList: e.data.data
                            })
                        }
                    })
                }
            }
        })
    },

    addUserTap: function () {
        wx.navigateTo({
            url: '/pages/enterprise/auth/user'
        })
    },

    updateUserTap: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    searchPostInput: function (e) {
        this.data.queryPostParams.postName = e.detail.value;
        this.data.postList = [];

        this.getPostList(this.data.queryPostParams);
    },

    /** 获取岗位信息*/
    getPostList: function (params) {
        let self = this;
        app.fetchApis(self, qCloud.api.getPostList, params, "GET", function (e) {
            if (e.data.code == 200) {
                let postList = e.data.rows;
                postList = [...self.data.postList, ...postList]
                self.setData({
                    postList: postList
                })
            }
        })
    },

    /**岗位长按删除*/
    postLongTap: function (e) {
        let self = this;
        self.setData({
            queryPostParams: {
                pageNum: 1,
                pageSize: 20,
                postName: ''
            },
            postList: []
        });

        let url = qCloud.api.deletePost + e.currentTarget.dataset.postid;
        wx.showModal({
            content: '确定要删除岗位吗？',
            success(res) {
                if (res.confirm) {
                    app.fetchApis(self, url, {}, "POST", function (e) {
                        if (e.data.code == 200) {
                            app.fetchApis(self, qCloud.api.getPostList, self.data.queryPostParams, "GET", function (e) {
                                if (e.data.code == 200) {
                                    let postList = e.data.rows;
                                    postList = [...self.data.postList, ...postList]
                                    self.setData({
                                        postList: postList
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: "error"
                            })
                        }
                    })
                } else if (res.cancel) {
                    app.fetchApis(self, qCloud.api.getPostList, self.data.queryPostParams, "GET", function (e) {
                        if (e.data.code == 200) {
                            let postList = e.data.rows;
                            postList = [...self.data.postList, ...postList]
                            self.setData({
                                postList: postList
                            })
                        }
                    })
                }
            }
        })
    },

    addPostTap: function () {
        wx.navigateTo({
            url: '/pages/enterprise/auth/post'
        })
    },

    updatePostTap: function (e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        })
    },

    postInput: function (e) {
        this.setData({
            postName: e.detail.value
        })
    },

    checkboxChange: function (e) {
        this.setData({
            manageMenuIds: e.detail.value
        })
    },

    departTab: function (e) {
        let self = this;
        wx.showModal({
            title: '提示',
            content: '确定要解聘员工吗？解聘后无法查看该企业任何信息，请谨慎操作',
            complete: (res) => {
                if (res.confirm) {
                    app.fetchApis(self, qCloud.api.departUser + e.currentTarget.dataset.userid, {}, "POST", function (e) {
                        if (e.data.code == 200) {
                            app.fetchApis(self, qCloud.api.getMyOrganUserList, {}, "GET", function (e) {
                                if (e.data.code == 200) {
                                    self.setData({
                                        userList: e.data.data
                                    })
                                }
                            })
                        } else {
                            wx.showToast({
                                title: e.data.msg,
                                icon: "error"
                            })
                        }
                    });
                }
            }
        })
    }
})
